package rxjava.operators;

import io.reactivex.*;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * User: Administrator
 * Date: 2021/3/24
 * Time: 14:05
 * Description:
 */
public class FlatMapDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();

        Observable.create((ObservableOnSubscribe<Integer>) observableEmitter -> {
            observableEmitter.onNext(1);
            observableEmitter.onNext(2);
            observableEmitter.onNext(3);
            observableEmitter.onNext(4);
            observableEmitter.onNext(5);
        }).flatMap(integer -> {
            list.add("i am" + integer);
            int delayTime = (int) (1 + Math.random() * 10);
            return Observable.fromIterable(list).delay(delayTime, TimeUnit.MILLISECONDS);
        }).subscribeOn(Schedulers.newThread())
                .subscribe(s -> System.out.println(s));
    }
}
